package com.luo.partner.controller;

import com.luo.common.controller.BaseController;
import com.luo.common.utils.PageUtils;
import com.luo.common.utils.Query;
import com.luo.common.utils.R;
import com.luo.common.utils.ShiroUtils;
import com.luo.partner.dao.model.TabRed;
import com.luo.partner.pojo.TabRedDO;
import com.luo.partner.service.TabRedService;
import com.luo.system.domain.UserDO;
import com.luo.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/partner/red")
public class TabRedController extends BaseController {
    @Autowired
    private TabRedService tabRedService;

    @Autowired
    private UserService userService;

    /**
     * 跳转列表页
     */
    @GetMapping()
    @RequiresPermissions("partner:red:list")
    public String tabRed() {
        return "partner/red/list";
    }

    /**
     * 列表页数据
     *
     * @param params 参数
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("partner:red:list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        UserDO currUser = ShiroUtils.getUser();
        UserDO userDO = userService.get(currUser.getUserId());
        List<Long> roleIds = userDO.getRoleIds();
        //如果用户不是超级管理员
        if (!roleIds.contains(1L)) {
            //是合伙人管理员
            if (roleIds.contains(59L)) {
                params.put("super_user_id", currUser.getUserId());
            } else {
                params.put("user_id", currUser.getUserId());
            }
        }
        Query query = new Query(params);
        List<TabRedDO> tabRedList = tabRedService.list(query);
        int total = tabRedService.count(query);
        return new PageUtils(tabRedList, total);
    }

    /**
     * 跳转添加页
     */
    @GetMapping("/add")
    @RequiresPermissions("partner:red:add")
    public String add(Model model) {
        UserDO currUser = ShiroUtils.getUser();
        HashMap<String, Object> params = new HashMap<>();
        UserDO userDO = userService.get(currUser.getUserId());
        List<Long> roleIds = userDO.getRoleIds();

        List<UserDO> userDOList = new ArrayList<>();
        //如果用户不是超级管理员
        if (roleIds.contains(1L)) {
            userDOList = userService.list(params);
            model.addAttribute("list", userDOList);
            return "partner/red/add";
        }

        //是合伙人管理员
        if (roleIds.contains(59L)) {
            params.put("super_user_id", currUser.getUserId());
            userDOList = userService.list(params);
            model.addAttribute("list", userDOList);
            return "partner/red/add";
        }
        model.addAttribute("list", userDOList);
        return "partner/red/add";
    }

    /**
     * 跳转编辑页
     *
     * @param redId 红包ID
     * @param model 实体
     */
    @GetMapping("/edit/{redId}")
    @RequiresPermissions("partner:red:edit")
    public String edit(@PathVariable("redId") Integer redId, Model model) {
        TabRed tabRed = tabRedService.get(redId);
        model.addAttribute("red", tabRed);
        return "partner/red/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("partner:red:add")
    public R save(TabRed tabRed) {
        if (tabRed.getUserId() == null) {
            UserDO currUser = ShiroUtils.getUser();
            tabRed.setUserId(currUser.getUserId());
        }
        if (tabRedService.save(tabRed) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("partner:red:edit")
    public R update(TabRed tabRed) {
        tabRedService.update(tabRed);
        return R.ok();
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("partner:red:remove")
    public R remove(Integer redId) {
        if (tabRedService.remove(redId) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("partner:red:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] red_ids) {
        tabRedService.batchRemove(red_ids);
        return R.ok();
    }

}
